package com.yxtbd.travel.travelvm.model.repository

import com.yxtbd.travel.travelvm.model.api.BaseRepository
import com.yxtbd.travel.travelvm.model.api.TravelRetrofitClient
import com.yxtbd.travel.travelvm.model.bean.LoginBean
import com.yxtbd.travel.travelvm.model.bean.ApiBaseResult
import com.yxtbd.travel.travelvm.util.MD5Util

class LoginRepository : BaseRepository() {

    suspend fun login(userName: String, passWord: String): ApiBaseResult<LoginBean> {
        return safeApiCall(call = { requestLogin(userName, passWord) }, errorMessage = "登录失败!")
    }

    private suspend fun requestLogin(userName: String, passWord: String): ApiBaseResult<LoginBean> {
        val map = HashMap<String, String>()
        map["username"] = userName
        map["password"] = MD5Util.encode(passWord)
        map["grant_type"] = "password"
        val loginBean = TravelRetrofitClient.service.login(map)
        return ApiBaseResult.Success(loginBean)

    }

}
